import { Component, Injector, ViewChild, Input } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';

import { WorkflowServiceProxy, UserServiceProxy, GetWorkflowInstanceViewDto, OutsourcingWorkFlowServiceProxy, SupplierQuotationServiceProxy, SupplierQuotationModel } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import * as _ from 'lodash';
import * as moment from 'moment';
import { from } from 'rxjs';

@Component({
  selector: 'awaitQuatationSupplier',
  templateUrl: './AwaitQuatationSupplier.component.html',
  styleUrls: ['./AwaitQuatationSupplier.component.css']
})
export class AwaitQuatationSupplierComponent extends AppComponentBase {
  @Input() suppliername: any;
  @Input() workflowid: any;

  isLoading = true;
  rows: number;
  supplierquotations: SupplierQuotationModel[];
  clonedOutsourcings: { [s: number]: SupplierQuotationModel; } = {};
  constructor(injector: Injector,
    private _supplierquotationService: SupplierQuotationServiceProxy) { super(injector); }

  ngOnInit() {

    console.log(this.workflowid);
    this.initForm();
  }


  initForm() {
    console.log(this.workflowid);
    this._supplierquotationService.getSupplierQuotations(this.workflowid, this.suppliername).subscribe(dtos => {
      this.supplierquotations = dtos;
      this.rows = this.supplierquotations.length;
      this.isLoading = false;
    });

  }

  onRowEditInit(quotationmodel: SupplierQuotationModel) {
    console.log(quotationmodel);
    this.clonedOutsourcings[quotationmodel.taskForModelId.toString()] = { ...quotationmodel };
  }

  onRowEditSave(quotationmodel: SupplierQuotationModel) {


console.log(quotationmodel);
    if (quotationmodel.outsouringAmount > 0) {
      delete this.clonedOutsourcings[quotationmodel.taskForModelId.toString()];

      this._supplierquotationService.createOrUpdateSupplierQuotation(quotationmodel).subscribe(result => {
        if (result == "ok") {
          this.notify.info(this.l('SavedSuccessfully'));
        }


      })
    } else {
      this.notify.info('申请数量不能小于0');
    }
  }

  onRowEditCancel(quotationmodel: SupplierQuotationModel, index: number) {
    console.log(this.clonedOutsourcings[quotationmodel.taskForModelId.toString()]);
    this.supplierquotations[index] = this.clonedOutsourcings[quotationmodel.taskForModelId.toString()];
    delete this.supplierquotations[quotationmodel.taskForModelId];
  }

}
